package com.wander.juc.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierExemple {
    public static void main(String[] args) {
        int count = 3;
        CyclicBarrier barrier = new CyclicBarrier(count,()->{
            System.out.println("线程当前阶段结束，进入下一阶段");
        });
        for (int i = 0; i < count; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName() + " 完成阶段1");
                    barrier.await();
                    System.out.println(Thread.currentThread().getName() + " 完成阶段2");
                    barrier.await();
                    System.out.println(Thread.currentThread().getName() + " 完成阶段3");
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
    }
}
